/*
 * @Author: licc
 * @Date: 2021-03-09 15:01:09
 * @Last Modified by: jinqiang
 * @Last Modified time: 2021-10-11 19:15:42
 */

import { VuexModule, Module, Mutation, Action, getModule } from 'vuex-module-decorators'
import { commonAction } from '@/utils/common'
import store from '@/store'
export interface MessageState {
  activeMsgListObj: any
}

@Module({ dynamic: true, name: 'message', store, namespaced: false })
class Message extends VuexModule implements MessageState {
  public activeMsgListObj: any = {
    // system: [], // 系统消息
    // systemCount: 0,
    // systemNoReadCount: 0,
    transmit: [], // 系统消息
    transmitCount: 0,
    workorderNotice: [],
    workorderNoticeCount: 0,
    workorderNoticeNoReadCount: 0,
    transmitNoReadCount: 0,
    star: [], // 星标消息
    starCount: 0,
    starNoReadCount: 0,
    notice: [],
    noticeCount: 0,
    noticeNoReadCount: 0,
    mpNotice: [],
    mpNoticeCount: 0,
    mpNoticeNoReadCount: 0,
    allCount: 0, // 所有类型消息总数
    allNoReadCount: 0, // 所有类型未读消息总数
    qualityCheck: [],
    qualityCheckCount: 0,
    qualityCheckNoReadCount: 0
  }

  @Action
  public async MessageCommonAction (params: object) {
    return commonAction(params).then(resp => {
      return resp
    })
  }

  /** 初始化消息列表 或 渲染消息列表 公共方法 */
  @Mutation
  public async SET_ACTIVE_XIAO_XI_PANE ({ type, list, count, noReadCount, reset = true }) {
    list.map(v => {
      v.isLoading = false
      v.isExpand = false
      return v
    })
    if (reset) {
      this.activeMsgListObj[type] = list || []
    } else {
      this.activeMsgListObj[type].push(...list)
    }
    this.activeMsgListObj[type + 'Count'] = count || 0
    this.activeMsgListObj[type + 'NoReadCount'] = noReadCount || 0
    // 有了其他类型，需要更新总count
    const { transmitCount, noticeCount, mpNoticeCount, qualityCheckCount, workorderNoticeCount, starCount, transmitNoReadCount, noticeNoReadCount, mpNoticeNoReadCount, qualityCheckNoReadCount, workorderNoticeNoReadCount, starNoReadCount } = this.activeMsgListObj
    this.activeMsgListObj.allCount = transmitCount + noticeCount + mpNoticeCount + qualityCheckCount + workorderNoticeCount + starCount
    this.activeMsgListObj.allNoReadCount = transmitNoReadCount + noticeNoReadCount + mpNoticeNoReadCount + qualityCheckNoReadCount + workorderNoticeNoReadCount + starNoReadCount
  }

  /** socket推送的消息 */
  @Mutation
  public async SET_SOCKET_PUSH_MESSAGE (msg) {
    msg.isLoading = false
    msg.isExpand = false
    this.activeMsgListObj[msg.type].unshift(msg)
  }

  /** 已读某条消息，未读数-1 */
  @Mutation
  public async SUBTRACT_TYPE_NO_READ_COUNT (msg) {
    this.activeMsgListObj[msg.type + 'NoReadCount']--
    this.activeMsgListObj['all' + 'NoReadCount']--
  }

  /** socket推送消息，全部未读数+1 */
  @Mutation
  public async ADD_ALL_NO_READ_COUNT () {
    this.activeMsgListObj['all' + 'NoReadCount']++
  }

  /** 已读所有消息后，清空所有未读消息数 */
  @Mutation
  public async CLEAR_NO_READ_COUNT () {
    this.activeMsgListObj['transmit' + 'NoReadCount'] = 0
    this.activeMsgListObj['star' + 'NoReadCount'] = 0
    this.activeMsgListObj['notice' + 'NoReadCount'] = 0
    this.activeMsgListObj['mpNotice' + 'NoReadCount'] = 0
    this.activeMsgListObj['all' + 'NoReadCount'] = 0
    this.activeMsgListObj['qualityCheck' + 'NoReadCount'] = 0
    this.activeMsgListObj['workorderNotice' + 'NoReadCount'] = 0
  }

  /** 打开消息通知，未读消息数 */
  @Mutation
  public async SET_NO_READ_COUNT (nums: any) {
    this.activeMsgListObj['transmit' + 'NoReadCount'] = nums.transmit
    this.activeMsgListObj['star' + 'NoReadCount'] = nums.star
    this.activeMsgListObj['notice' + 'NoReadCount'] = nums.notice
    this.activeMsgListObj['mpNotice' + 'NoReadCount'] = nums.mpNotice
    this.activeMsgListObj['workorderNotice' + 'NoReadCount'] = nums.workorderNotice
    this.activeMsgListObj['qualityCheck' + 'NoReadCount'] = nums.qualityCheck
    this.activeMsgListObj['all' + 'NoReadCount'] = nums.transmit + nums.star + nums.notice + nums.mpNotice + nums.workorderNotice + nums.qualityCheck
  }
}

export const MessageModule = getModule(Message)
